package org.kinglone.back.loginLog.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kinglone.back.loginLog.model.LoginLog;
import org.kinglone.back.loginLog.service.LoginLogService;
import org.kinglone.common.util.JSONMessageView;
import org.kinglone.common.util.PageQuery;
import org.kinglone.common.util.PageResults;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 登录日志控制层
 * @author yejinlong
 * 2017年7月25日
 * @version 1.0
 */
@Controller
@RequestMapping("/loginlog")
public class LoginLogAction {
	
	@Resource(name="loginLogService")
	private LoginLogService loginLogService;

	//跳转到登录日志页面
	@RequestMapping("/loginlog.jspx")
	public ModelAndView loginLog(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/WEB-INF/jsp/back/log/loginlog.jsp");
		return mv;
	}

	/**
	 * 分页查询日志
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectLoginLog.jspx")
	public JSONMessageView selectLoginLog(HttpServletRequest request) {
		JSONMessageView json = new JSONMessageView(-1, "查询失败!", null);
		try {
			//当前页
			String pageNo = request.getParameter("pageNo"); 
			//每页大小
			String pageSize = request.getParameter("pageSize"); 						
			PageQuery pageQuery=new PageQuery();		
			pageQuery.setStart((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
			pageQuery.setLength(Integer.parseInt(pageSize));			
			PageResults<LoginLog> pageResults = loginLogService.getLoginLog(pageQuery);					
			if (pageResults != null){
				JSONArray totalJsonArray = new JSONArray();
				JSONObject totalJsonObject = new JSONObject();
				JSONArray jsonArray=new JSONArray();
				for(LoginLog log : pageResults.getResults()){
					JSONObject dataTemp =new JSONObject();
					dataTemp.put("sysid",log.getSysid());
					dataTemp.put("username", log.getUsername());
					dataTemp.put("loginTime", log.getLoginTime());
					dataTemp.put("remoteIP", log.getRemoteIP());
					dataTemp.put("clientIP", log.getClientIP());
					dataTemp.put("status", log.getStatus());
					dataTemp.put("pagetotal", pageResults.getTotalCount());
					jsonArray.add(dataTemp);
				}
				totalJsonObject.put("pagetotal", pageResults.getPageCount());//总页数
				totalJsonObject.put("info", jsonArray);
				totalJsonArray.add(totalJsonObject);
				json.setCode(0);
				json.setMessage("查询成功!");
				json.setContent(totalJsonArray);
				return json;	
			}else{
				json.setCode(-1);
				json.setMessage("暂无数据!");
				return json;
			}						
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONMessageView(-1, "查询失败!", null);
		}		
	}
	/**
	 * 根据id删除日志
	 * @param request
	 * @return
	 */
	@RequestMapping("/delLoginlog.jspx")
	public JSONMessageView delLoginlog(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1,"删除失败!",null);
		try{
			String logId = request.getParameter("logId");
			loginLogService.deleteById(logId);
			json.setCode(0);
			json.setMessage("删除成功!");
			return json;
		}catch (Exception e){
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("服务器异常,请稍后再试!");
			return json;
		}
	}

	/**
	 * 批量删除日志
	 * @param request
	 * @return
	 */
	@RequestMapping("/delMoreLog.jspx")
	public JSONMessageView delMoreLog(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1,"删除失败!",null);
		try {
			String ids = request.getParameter("ids");
			String[] idArr = ids.split(",");
			for(int i = 0;i < idArr.length;i++){
				loginLogService.deleteById(idArr[i]);
			}
			json.setCode(0);
			json.setMessage("删除成功!");
			return json;
		}catch (Exception e){
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("服务器异常,请稍候再试!");
			return json;
		}
	}


}
